package online.inote.common.data.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import online.inote.common.data.valid.ByteLength;
import online.inote.common.utils.StringUtils;

/**
 * <p>
 * Desc: 
 * </p>
 *
 * @author XQF SUI
 * @created 2018年9月4日 下午11:25:02
 * @version 1.0
 */
public class ByteLengthValidator implements ConstraintValidator<ByteLength, String> {

	private static int MIN, MAX; 
	
	@Override
    public void initialize(ByteLength length) {
        MIN = length.min();
        MAX = length.max();
	}
	
	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {

		if (StringUtils.isBlank(value)) {
			if (MIN == 0) {
				return true;
			} else {
				return false;
			}
		}
		
		if (StringUtils.byteLength(value) > MAX) {
			return false;
		}
		
		return true;
	}

}
